package com.example.demo.nine;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
public class Producer {
    public static final String QUEUE_NAME = "node2_queue";
    public static final String EXCHANGE_NAME = "fed_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.255.134");
        factory.setUsername("admin");
        factory.setPassword("123");
        // 获取连接
        Connection conn = factory.newConnection();
        // 创建信道
        Channel channel = conn.createChannel();
        // 生成队列

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"rk");


        // boolean durable = true; // 队列持久化
        // Map<String, Object> arguments = new HashMap<>();
        // arguments.put("x-max-priority", 10);// 0-255
        // channel.queueDeclare(QUEUE_NAME, durable, false, false, arguments);
        //
        // for (int i = 1; i < 11; i++) {
        //     String msg = "info" + i;
        //     if (i == 5) {
        //         AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(5).build();
        //         channel.basicPublish("", QUEUE_NAME, properties, msg.getBytes());
        //     } else {
        //         channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        //     }
        // }
        // String message = "Hello Word";
        // // 消息持久化
        // // channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        // System.out.println("消息发送完毕");
    }
}
